package com.itheima.a01mystream;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamDemo10 {
    public static void main(String[] args) {
        /**
         * collect
         */

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌-男-13", "张无忌-男-13", "周芷若-女-17", "赵敏-女-21", "张强-男-23", "张三丰-男-27", "张翠流-女-31", "张良-男-34", "王二麻子-男-37", "谢广坤-男-41");

        // collect(Collector collector) 收集流中的数据，放到集合中（List Set Map）

        // 收集男性
        System.out.println("toList");
        List<String> list1 = list.stream()
                .filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toList());
        System.out.println(list1);

        System.out.println("toSet");
        Set<String> set = list.stream()
                .filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toSet());
        System.out.println(set);

        System.out.println("toMap");
        // Map<String, Integer> map = set.stream()
        //         .filter(s -> "男".equals(s.split("-")[1]))
        //         .collect(Collectors.toMap(
        //                 new Function<String, String>() {
        //                     @Override
        //                     public String apply(String s) {
        //                         return s.split("-")[0];
        //                     }
        //                 },
        //                 new Function<String, Integer>() {
        //                     @Override
        //                     public Integer apply(String s) {
        //                         return Integer.parseInt(s.split("-")[2]);
        //                     }
        //                 }
        //         ));
        Map<String, Integer> map = set.stream()
                .collect(Collectors.toMap(
                        s -> s.split("-")[0],
                        s -> Integer.parseInt(s.split("-")[2])
                ));
        System.out.println(map);
    }
}
